package com.lc.hongtai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonInvestdetail;
import com.zcx.helper.bound.BoundViewHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 4/18/2016.
 */
public class ViewPagerListAdapter3 extends RecyclerView.Adapter<ViewPagerListAdapter3.ViewHolder> {

    private Context context;
    private List<Object> list;
    private int mark;

    public ViewPagerListAdapter3(Context context, List<Object> list, int mark) {

        this.context = context;
        this.list = list;
        this.mark = mark;
    }

    // Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        @Bind(R.id.tv_01)
        TextView tv01;
        @Bind(R.id.tv_02)
        TextView tv02;
        @Bind(R.id.tv_03)
        TextView tv03;
        @Bind(R.id.tv_04)
        TextView tv04;
        @Bind(R.id.tv_05)
        TextView tv05;
        @Bind(R.id.tv_06)
        TextView tv06;
        @Bind(R.id.ll_01)
        LinearLayout ll01;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    /**
     * Called when RecyclerView needs a new {@link ViewHolder} of the given type to represent
     * an item.
     * <p/>
     * This new ViewHolder should be constructed with a new View that can represent the items
     * of the given type. You can either create a new View manually or inflate it from an XML
     * layout file.
     * <p/>
     * The new ViewHolder will be used to display items of the adapter using
     * {@link #onBindViewHolder(ViewHolder, int)}. Since it will be re-used to display
     * different items in the data set, it is a good idea to cache references to sub views of
     * the View to avoid unnecessary {@link View#findViewById(int)} calls.
     *
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return A new ViewHolder that holds a View of the given view type.
     * @see #getItemViewType(int)
     * @see #onBindViewHolder(ViewHolder, int)
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_14_item_view003, null)));
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    /**
     * Called by RecyclerView to display the data at the specified position. This method should
     * update the contents of the {@link ViewHolder#itemView} to reflect the item at the given
     * position.
     * <p/>
     * Note that unlike {@link  ListView}, RecyclerView will not call this method
     * again if the position of the item changes in the data set unless the item itself is
     * invalidated or the new position cannot be determined. For this reason, you should only
     * use the <code>position</code> parameter while acquiring the related data item inside
     * this method and should not keep a copy of it. If you need the position of an item later
     * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will
     * have the updated adapter position.
     * <p/>
     * Override {@link #onBindViewHolder(ViewHolder, int)} instead if Adapter can
     * handle effcient partial bind.
     *
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mark == 0) {
            //投资记录

            holder.tv01.setText("干他妈的");
            String name = ((JsonInvestdetail.Info.InvestListContent) list.get(position)).user_name;
         //   holder.tv02.setText(name.substring(0, 1) + "***" + name.substring(name.length() - 1, name.length()));
            holder.tv02.setText("干他妈的" +
                    "");
            holder.tv03.setText(((JsonInvestdetail.Info.InvestListContent) list.get(position)).investor_capital);
            holder.tv04.setText(((JsonInvestdetail.Info.InvestListContent) list.get(position)).investor_interest);
            holder.tv05.setText(((JsonInvestdetail.Info.InvestListContent) list.get(position)).is_auto);
            holder.tv06.setText(((JsonInvestdetail.Info.InvestListContent) list.get(position)).add_time);
        } else if (mark == 1) {
            // holder.tv01.setText((position+1)+"");
            holder.tv01.setVisibility(View.VISIBLE);

            // holder.tv02.setVisibility(View.GONE);

            //债权转让 界面adapter

            String name = "借款标题：" + ((JsonInvestdetail.Info.PayingListContent) list.get(position)).borrow_name;
            SpannableString spannableString = new SpannableString(name);
            //  spannableString.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 1, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //正常

            spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.text_hint_color)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色
            holder.tv01.setText(spannableString);


            String str2 = "借款标题: " + name.substring(0, 1) + "***" + name.substring(name.length() - 1, name.length());
            SpannableString spannableString2 = new SpannableString(str2);
            spannableString2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.text_hint_color)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色


            holder.tv02.setText(spannableString2);

            String str3 = "利息: " + ((JsonInvestdetail.Info.PayingListContent) list.get(position)).capital;
            SpannableString spannableString3 = new SpannableString(str3);
            spannableString3 .setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.text_hint_color)), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色
            holder.tv03.setText(spannableString3);

            String str4 = "还款本金: " + ((JsonInvestdetail.Info.PayingListContent) list.get(position)).interest;
            SpannableString spannableString4 = new SpannableString(str4);
            spannableString4.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.text_hint_color)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色


            holder.tv04.setText(spannableString4);


            String str5 = "还款状态: " + ((JsonInvestdetail.Info.PayingListContent) list.get(position)).status;
            SpannableString spannableString5 = new SpannableString(str5);
            spannableString5.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.text_hint_color)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色


            holder.tv05.setText(spannableString5);


            String str6 = "到期时间: " + ((JsonInvestdetail.Info.PayingListContent) list.get(position)).deadline_cn;
            SpannableString spannableString6 = new SpannableString(str6);
            spannableString6.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.text_hint_color)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色

            holder.tv06.setText(spannableString6);


        }
        if (position % 2 == 1) {
            holder.itemView.setBackgroundResource(R.color.colorWhite);
        } else {
            //holder.itemView.setBackgroundResource(R.color.colorGreydividingline);
            holder.itemView.setBackgroundResource(R.color.colorWhite);
        }
    }

    /**
     * Returns the total number of items in the data set hold by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        if (list.size() < 10) {
            return list.size();
        } else {
            return 10;
        }


    }
}